package com.yungu.swift.system.sys.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.lbs.model.vo.FenceVo;
import com.yungu.swift.lbs.service.FenceService;
import com.yungu.swift.system.sys.dao.SysWayFenceMapper;
import com.yungu.swift.system.sys.model.dto.SysWayFenceDto;
import com.yungu.swift.system.sys.model.param.FenceRaisePriceParam;
import com.yungu.swift.system.sys.model.vo.FenceRaisePriceVo;
import com.yungu.swift.system.sys.model.vo.WayfenceInfoVo;
import com.yungu.swift.system.sys.service.SysWayFenceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 拼车围栏设置
 */
@Slf4j
@Service
public class SysWayFenceServiceImpl extends BaseServiceImpl<SysWayFenceDto> implements SysWayFenceService {

    @Autowired
    private SysWayFenceMapper sysWayFenceMapper;
    @Reference
    private FenceService fenceService;

    @Override
    protected IMapper<SysWayFenceDto> getMapper() {
        return sysWayFenceMapper;
    }

    @Override
    public ResponseData<FenceRaisePriceVo> queryFenceRaisePrice(FenceRaisePriceParam priceParam) {
        //围栏ID集合
        List<String> fenceGidList = new ArrayList<>();
        try {
            //获取起点围栏 DIU 参数不影响结果 暂时服务端临时指定
            getFenceGidList(fenceService.statusFence(priceParam.getOriginLng(), priceParam.getOriginLat(), "861144047871995").getData(), fenceGidList);
            //获取终点围栏
            getFenceGidList(fenceService.statusFence(priceParam.getDestLng(), priceParam.getDestLat(), "861144047871995").getData(), fenceGidList);
        } catch (Exception e) {
            log.error("获取围栏信息异常", e);
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "获取围栏信息异常");
        }
        if (fenceGidList.size() == 0) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "起终点未进入特殊区域");
        }
        //通过线路ID/围栏ID获取围栏加价信息
        Map<String, Object> paramMap = new HashMap<>(4);
        paramMap.put("fenceGidList", fenceGidList);
        paramMap.put("wayUuid", priceParam.getWayUuid());
        List<SysWayFenceDto> wayFenceList = sysWayFenceMapper.list(paramMap);
        if (CollectionUtils.isEmpty(wayFenceList)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "起终点未进入该路线的特殊区域");
        }
        //取出加价信息
        FenceRaisePriceVo fenceRaisePriceVo = new FenceRaisePriceVo();
        for (SysWayFenceDto sysWayFenceDto : wayFenceList) {
            //起点加价
            if (sysWayFenceDto.getSiteUuid().equals(priceParam.getOriginUuid())) {
                fenceRaisePriceVo.setOriginRaisePrice(sysWayFenceDto.getRaisePrice());
            }
            //终点加价
            if (sysWayFenceDto.getSiteUuid().equals(priceParam.getDestUuid())) {
                fenceRaisePriceVo.setDestRaisePrice(sysWayFenceDto.getRaisePrice());
            }
        }
        return ResponseData.buildSuccessResponse(fenceRaisePriceVo);
    }

    /**
     * 取出围栏ID集合
     */
    private void getFenceGidList(List<FenceVo> fenceVos, List<String> fenceGidList) throws Exception {
        if (CollectionUtils.isNotEmpty(fenceVos)) {
            List<String> collect = fenceVos.stream().filter(fenceVo -> "in".equals(fenceVo.getStatus())).map(fenceVo ->
                    fenceVo.getFenceGid()).collect(Collectors.toList());
            fenceGidList.addAll(collect);
        }
    }

    /**
     * 查询线路围栏信息
     */
    @Override
    public ResponseData<List<WayfenceInfoVo>> queryWayFenceInfo(String wayUuid) {
        return ResponseData.buildSuccessResponse(sysWayFenceMapper.queryWayFenceInfo(wayUuid));
    }
}
